package com.oil.petro.eshop.service;

import com.oil.petro.eshop.bean.ProductBean;
import com.oil.petro.eshop.bean.UserBean;
import com.oil.petro.eshop.dao.AccountDAO;
import com.oil.petro.eshop.dao.AccountDAOImpl;
import com.oil.petro.eshop.dao.OrderDAO;
import com.oil.petro.eshop.dao.PersonDAO;
import com.oil.petro.eshop.dao.ProductDAO;
import com.oil.petro.eshop.entity.Account;
import com.oil.petro.eshop.entity.Person;
import com.oil.petro.eshop.entity.Product;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

//@Service("userAccount")
@Service
@Transactional()/**/

public class UserAccountServiceImpl implements UserAccountService {

    private static final int ANONYMOUS_USER = 0;
    private static final int LOGGED_USER = 1;
    private static final int LOGGED_ADMINISTRATOR = 2;

    // AccountDAO accountDAO;
    @Autowired
    AccountDAO accountDAO;

    @Autowired
    PersonDAO personDAO;

    @Autowired
    ProductDAO productDAO;

    @Autowired
    OrderDAO orderDAO;

    //private AccountDAO accountDAO;
//    public int registerUserAccouny(Account account) {
//        accountDAO = new AccountDAO();
//        return accountDAO.add(account);
//    }
//
//    public void updateUserAccount(Account account) {
//        accountDAO = new AccountDAO();
//        accountDAO.update(account);
//    }
//
//    public void deleteUserAccount(Account account) {
//        accountDAO = new AccountDAO();
//        accountDAO.delete(account);
//    }
//
//    public Account getUserAccount(int id) {
//        accountDAO = new AccountDAO();
//       return   accountDAO.get(id);
//    }
//   @Transactional
//    public void addAccount(Account account) {
//        persistanceAccount.saveAccount(account);
//       // throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
//    }
//
//      @Transactional
//    public Account getAccount(int id) {
//        
//        return persistanceAccount.get(id);
//        //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
//    }
    @Transactional
    @Override
    public Person getPerson(int id) {
        return personDAO.get(id);
        // throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Transactional
    @Override
    public void deletePerson(Person person) {
        personDAO.delete(person);
        // throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Transactional
    @Override
    public Integer savePerson(Person person) {
        return personDAO.save(person);
        //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Transactional
    @Override
    public void updatePerson(Person person) {
        personDAO.update(person);
        //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Transactional
    @Override
    public Account getAccount(int id) {
        return accountDAO.get(id);
        //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Transactional
    @Override
    public void deleteAccount(Account account) {
        accountDAO.delete(account);
        //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Transactional
    @Override
    public int saveAccount(Account account) {
        return accountDAO.save(account);
        //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Transactional
    @Override
    public void updateAccount(Account account) {
        accountDAO.update(account);
        //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Transactional
    @Override
    public void saveUser(UserBean userBean) {

        Account account = new Account(userBean);
        Person person = new Person(userBean);

        Integer personId = personDAO.save(person);
        //personDAO.g
        System.out.println("AAAA " + personId);
        account.setPersonId(personId);
        account.setRole("ROLE_USER");
        accountDAO.save(account);

    }

    @Transactional
    @Override
    public Account getAccountByLogin(String login) {
        Account account = accountDAO.getByLogin(login);

        if (account == null) {
            throw new UsernameNotFoundException("Account not found for user " + login);
        }
        return account;
    }

    @Transactional
    @Override
    public int getAccountStatus(String login) {

        Account account = accountDAO.getByLogin(login);

        if ("ROLE_USER".equals(account.getRole())) {
            return LOGGED_USER;
        } else if ("ROLE_ADMIN".equals(account.getRole())) {
            return LOGGED_ADMINISTRATOR;
        } else {
            return ANONYMOUS_USER;
        }

    }

    public int getNumberProductsInCart(String login) {

        Account account = accountDAO.getByLogin(login);
        return orderDAO.getOrderedProducts(account.getId()).size();

    }

//     @Transactional
//    @Override
//     public void addProductToCart(Account account, Product product){
//     
//     account.addProduct(product);
//            accountDAO.save(account);
//     }
    @Override
    public UserBean getUserDetails(String login) {
        Account account = accountDAO.getByLogin(login);
        Person person = personDAO.get(account.getPersonId());
        UserBean userBean = new UserBean();
        userBean.setFirstName(person.getFirstName());
        userBean.setLastName(person.getLastName());
        userBean.setAddress(person.getAddress());
        userBean.setCity(person.getCity());
        userBean.setPostCode(person.getPostCode());

        userBean.setLogin(account.getLogin());
        userBean.setEmail(account.getEmail());
        userBean.setPassword(account.getPassword());
        userBean.setRepassword(account.getPassword());
        return userBean;
    }

    public boolean isUniqueUser(String loigin) {

        Account account = (Account) getAccountByLogin(loigin);

        if (account == null) {
            return false;
        } else {
            return true;
        }

    }

}
